// Fill out your copyright notice in the Description page of Project Settings.


#include "MeshReader.h"
//#include "MeshReaderActor.h"
#include "EngineUtils.h"
#include "Engine/StaticMeshActor.h"
#include "Engine/World.h"
#include "StaticMeshResources.h"
#include "DrawDebugHelpers.h"

// Sets default values
AMeshReader::AMeshReader()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	USceneComponent* SceneRoot = CreateDefaultSubobject<USceneComponent>("Root");
	RootComponent = SceneRoot;

}

// Called when the game starts or when spawned
void AMeshReader::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMeshReader::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
    ScanSceneForMeshes(true);

}

void AMeshReader::ScanSceneForMeshes(bool bIncludeBSP)
{
    UE_LOG(LogTemp, Log, TEXT("Scanned %d mesh actors in scene"), 3);
    ScannedMeshActors.Empty();

    UWorld* World = GetWorld();
    if (!World)
    {
        UE_LOG(LogTemp, Warning, TEXT("Failed to get World!"));
        return;
    }

    // �������о�̬����Actor
    //for (TActorIterator<AActor> It(World); It; ++It)
    //{
    //    AActor* Actor = *It;

    //    // ����Ƿ��о�̬����������Ǿ�̬����Actor
    //    if (Actor->FindComponentByClass<UStaticMeshComponent>() || Actor->IsA<AStaticMeshActor>())
    //    {
    //        ScannedMeshActors.Add(Actor);
    //    }

    //    // ��ѡ����BSP������
    //    if (bIncludeBSP && Actor->FindComponentByClass<UBrushComponent>())
    //    {
    //        ScannedMeshActors.Add(Actor);
    //    }
    //}

    //UE_LOG(LogTemp, Log, TEXT("Scanned %d mesh actors in scene"), ScannedMeshActors.Num());
}
//
//int32 AMeshReader::GetScannedMeshCount() const
//{
//    return ScannedMeshActors.Num();
//}
//
//AActor* AMeshReader::GetScannedMeshActor(int32 Index) const
//{
//    if (ScannedMeshActors.IsValidIndex(Index))
//    {
//        return ScannedMeshActors[Index];
//    }
//    return nullptr;
//}
//
//TArray<FVector> AMeshReader::GetMeshVertices(AActor* TargetActor, int32 LODIndex) const
//{
//    TArray<FVector> Vertices;
//
//    if (!TargetActor)
//    {
//        UE_LOG(LogTemp, Warning, TEXT("TargetActor is null!"));
//        return Vertices;
//    }
//
//    UStaticMesh* StaticMesh = GetStaticMeshFromActor(TargetActor);
//    if (!StaticMesh || !StaticMesh->GetRenderData())
//    {
//        UE_LOG(LogTemp, Warning, TEXT("Failed to get StaticMesh or render data!"));
//        return Vertices;
//    }
//    if (StaticMesh->GetRenderData()->LODResources.Num() <= LODIndex)
//    {
//        UE_LOG(LogTemp, Warning, TEXT("Invalid LOD index: %d"), LODIndex);
//        return Vertices;
//    }
//
//    FStaticMeshLODResources& LODResource = StaticMesh->GetRenderData()->LODResources[LODIndex];
//    FPositionVertexBuffer& VertexBuffer = LODResource.VertexBuffers.PositionVertexBuffer;
//
//    const int32 NumVertices = VertexBuffer.GetNumVertices();
//    Vertices.Reserve(NumVertices);
//
//    for (int32 i = 0; i < NumVertices; ++i)
//    {
//        // ����������Ӿֲ��ռ�ת��������ռ�
//        FVector WorldVertex = TargetActor->GetTransform().TransformPosition(VertexBuffer.VertexPosition(i));
//        Vertices.Add(WorldVertex);
//    }
//
//    return Vertices;
//}
//
//void AMeshReader::VisualizeMeshVertices(AActor* TargetActor, float PointSize, FColor Color) const
//{
//    if (!TargetActor) return;
//
//    TArray<FVector> Vertices = GetMeshVertices(TargetActor);
//    UWorld* World = GetWorld();
//
//    for (const FVector& Vertex : Vertices)
//    {
//        DrawDebugPoint(World, Vertex, PointSize, Color, true);
//    }
//}
//
//UStaticMeshComponent* AMeshReader::GetStaticMeshComponent(AActor* Actor) const
//{
//    if (!Actor) return nullptr;
//
//    // ����ֱ�ӻ�ȡStaticMeshComponent
//    UStaticMeshComponent* MeshComponent = Actor->FindComponentByClass<UStaticMeshComponent>();
//
//    // ���û���ҵ�������Ƿ���AStaticMeshActor
//    if (!MeshComponent && Actor->IsA<AStaticMeshActor>())
//    {
//        MeshComponent = Cast<AStaticMeshActor>(Actor)->GetStaticMeshComponent();
//    }
//
//    return MeshComponent;
//}
//
//UStaticMesh* AMeshReader::GetStaticMeshFromActor(AActor* Actor) const
//{
//    UStaticMeshComponent* MeshComponent = GetStaticMeshComponent(Actor);
//    return MeshComponent ? MeshComponent->GetStaticMesh() : nullptr;
//}